package com.ruoyi.smart.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.smart.domain.*;
import com.ruoyi.smart.mapper.SmartPlanTaskMapper;
import com.ruoyi.smart.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.smart.mapper.SmartProductTestRecordMapper;

/**
 * 产品测试记录Service业务层处理
 * 
 * @author minwei
 * @date 2022-10-20
 */
@Service
public class SmartProductTestRecordServiceImpl implements ISmartProductTestRecordService 
{
    @Autowired
    private SmartProductTestRecordMapper smartProductTestRecordMapper;
    @Autowired
    private ISmartProductService smartProductService;
    @Autowired
    private ISmartPlanTaskService smartPlanTaskService;
    @Autowired
    private ISmartProductTestWaveformService smartProductTestWaveformService;
    @Autowired
    private SmartPlanTaskMapper smartPlanTaskMapper;

    /**
     * 查询产品测试记录
     * 
     * @param id 产品测试记录主键
     * @return 产品测试记录
     */
    @Override
    public SmartProductTestRecord selectSmartProductTestRecordById(Long id)
    {
        return smartProductTestRecordMapper.selectSmartProductTestRecordById(id);
    }

    /**
     * 查询产品测试记录列表
     * 
     * @param smartProductTestRecord 产品测试记录
     * @return 产品测试记录
     */
    @Override
    public List<SmartProductTestRecord> selectSmartProductTestRecordList(SmartProductTestRecord smartProductTestRecord)
    {
        return smartProductTestRecordMapper.selectSmartProductTestRecordList(smartProductTestRecord);
    }

    @Override
    public List<SmartProductTestRecord> selectSmartProductTestRecordList(String organization, String plan, String productModel, String testState){
        List<SmartProductTestRecord> list=new ArrayList<>();
        SmartProductTestRecord smartProductTestRecord=new SmartProductTestRecord();
        smartProductTestRecord.setTestState(testState);
        if(organization!=null&&!"".equals(organization))
        {
            smartProductTestRecord.setOrganizeorganazId(Long.parseLong(organization));
        }
        if(plan!=null&&!"".equals(plan))
        {
            smartProductTestRecord.setTaskPalnId(Long.parseLong(plan));
        }
        if(productModel!=null&&!"".equals(productModel))
        {
            smartProductTestRecord.setProductModelId(Long.parseLong(productModel));
        }
        list=smartProductTestRecordMapper.selectSmartProductTestRecordList(smartProductTestRecord);
//        if(organization==null&&"".equals(organization)&&plan==null&&"".equals(plan)&&productModel==null&&"".equals(productModel)){
//            list=smartProductTestRecordMapper.selectSmartProductTestRecordList(smartProductTestRecord);
//        }else {
//            if(productModel!=null&&"".equals(productModel)){
//                smartProductTestRecord.setProductId(Long.valueOf(productModel));
//                list.addAll(smartProductTestRecordMapper.selectSmartProductTestRecordList(smartProductTestRecord));
//            }else {
//                list=smartProductTestRecordMapper.selectSmartProductTestRecordList(smartProductTestRecord);
//            }
////            List<SmartProduct> list2=smartProductService.selectSmartProductList(organization,plan,productModel,new SmartProduct());
////            for(SmartProduct smartProduct : list2){
////                smartProductTestRecord.setProductId(smartProduct.getId());
////                list.addAll(smartProductTestRecordMapper.selectSmartProductTestRecordList(smartProductTestRecord));
////            }
//        }
        return list;
    }

    @Override
    public int insertInfo(){
        List<SmartProductTestRecord> list=smartProductTestRecordMapper.selectSmartProductTestRecordList1000(new SmartProductTestRecord());
        for(SmartProductTestRecord item : list){
            if(item.getSmartProduct()!=null){
                if(item.getSmartProduct().getModel()!=null&&!"".equals(item.getSmartProduct().getModel())){
                    item.setProductModelId(Long.valueOf(item.getSmartProduct().getModel()));
                }
                SmartPlanTask smartPlanTask=smartPlanTaskMapper.selectSmartPlanTaskById(item.getSmartProduct().getTaskId());
                if(smartPlanTask!=null){
                    item.setTaskPalnId(smartPlanTask.getId());
                    item.setOrganizeorganazId(smartPlanTask.getOrganizeorganazId());
                }
            }
            smartProductTestRecordMapper.updateSmartProductTestRecord(item);
        }
        return 0;
    }

    /**
     * 新增产品测试记录
     * 
     * @param smartProductTestRecord 产品测试记录
     * @return 结果
     */
    @Override
    public int insertSmartProductTestRecord(SmartProductTestRecord smartProductTestRecord)
    {
        smartProductTestRecord.setCreateTime(DateUtils.getNowDate());
        return smartProductTestRecordMapper.insertSmartProductTestRecord(smartProductTestRecord);
    }

    /**
     * 修改产品测试记录
     * 
     * @param smartProductTestRecord 产品测试记录
     * @return 结果
     */
    @Override
    public int updateSmartProductTestRecord(SmartProductTestRecord smartProductTestRecord)
    {
        smartProductTestRecord.setUpdateTime(DateUtils.getNowDate());
        return smartProductTestRecordMapper.updateSmartProductTestRecord(smartProductTestRecord);
    }

    /**
     * 批量删除产品测试记录
     * 
     * @param ids 需要删除的产品测试记录主键
     * @return 结果
     */
    @Override
    public int deleteSmartProductTestRecordByIds(Long[] ids)
    {
        return smartProductTestRecordMapper.deleteSmartProductTestRecordByIds(ids);
    }

    /**
     * 删除产品测试记录信息
     * 
     * @param id 产品测试记录主键
     * @return 结果
     */
    @Override
    public int deleteSmartProductTestRecordById(Long id)
    {
        return smartProductTestRecordMapper.deleteSmartProductTestRecordById(id);
    }

    /**
     * 设置产品生产的检验状态
     *
     * @param smartModData 输入的信息
     * 下面是mtr数据示例：
     *{
     *     "Mtr_Info":{
     *         "测试项目":"功能测试\n"
     *     },
     *     "data":[
     *         {
     *             "time":"2022-07-21_09:54:57.066",
     *             "switch id":"20001115",
     *             "cap":"on-line",
     *             "The average current":"371.03",
     *             "The average voltage":"57.56",
     *             "The highest current":"704.10",
     *             "The highest voltage":"78.20",
     *             "The Cascade current ":"1.50",
     *             "Result":"合格\n"
     *         },
     *         {
     *             "time":"2022-07-21_09:58:23.929",
     *             "switch id":"20001115",
     *             "cap":"on-line",
     *             "The average current":"\n",
     *             "The average voltage":" ",
     *             "The highest current":" ",
     *             "The highest voltage":" ",
     *             "The Cascade current ":" ",
     *             "Result":" "
     *         },
     *         {
     *             "time":"2022-07-21_09:58:25.633",
     *             "switch id":"20001122",
     *             "cap":"on-line",
     *             "The average current":"\n",
     *             "The average voltage":" ",
     *             "The highest current":" ",
     *             "The highest voltage":" ",
     *             "The Cascade current ":" ",
     *             "Result":" "
     *         }
     *     ],
     *     "type":"mtr"
     * }
     */

    @Override
    public void setState(SmartModData smartModData){
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS");
        JSONObject modStr = JSONObject.parseObject(smartModData.getModStr());
        String Mtr_Info = modStr.getString("测试项目");
        String testUser = modStr.getString("操作员");
        JSONArray jsonArray=JSONArray.parseArray(smartModData.getModList());
        for (int i = 0; i < jsonArray.size(); i++) {
            //1.保存产品检验状态
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String product_code = jsonObject.getString("switch id");
            String result = jsonObject.getString("Result");
            String time = jsonObject.getString("time");

            //首先查出对应的产品
            /*SmartProduct smartProduct=new SmartProduct();
            smartProduct.setCode(product_code);
            List<SmartProduct> smartProducts=smartProductService.selectSmartProductList(smartProduct);*/
//            if(smartProducts.size()>0){
            SmartProductTestRecord smartProductTestRecord=new SmartProductTestRecord();
            smartProductTestRecord.setProductId(Long.parseLong(product_code)); //设置关联的产品
            smartProductTestRecord.setTestName(Mtr_Info); //设置状态名称
            smartProductTestRecord.setTestState(result);   //设置检验结果
            smartProductTestRecord.setTestUesr(testUser);   //设置测试人
            try {
                smartProductTestRecord.setTestTime(formatter.parse(time));  //设置时间
            } catch (ParseException e) {e.printStackTrace();}
            //插入记录
            smartProductTestRecordMapper.insertSmartProductTestRecord(smartProductTestRecord);
            //添加波形数据


            //按照产品中的型号信息，查询产品的计划信息，修改计划完成状态。目前产品没有型号，无法查询产品计划
            SmartPlanTask smartPlan=smartPlanTaskService.getPlanByProductCode(product_code);
            if(smartPlan.getId()!=null&&smartProductTestRecord.getTestName()!=null){
                //判断是否是关键状态的信息。如果是就将产品的生产状态改为生产完成
                if(smartProductTestRecord.getTestName().contains("检验功能")){
                  //将计划的数量增加
                    smartPlanTaskService.addPlanQuality(smartPlan.getId(),1);
                }
            }

//            }
        }
    }

}
